package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.Impl.XiangQingDaoImpl;
import com.ysd.entity.Products;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminProductServlet extends BaseServlet {
	/*****
	 * 获取产品详情
	 *   localhost:8080/huaduoyoupin/adminProduct?reqName=getProduct
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接收前台传来的参数
		int productId=0;
		if(request.getParameter("productId")!=null && !request.getParameter("productId").equals("")) {
			productId=Integer.parseInt(request.getParameter("productId"));
		}
			
		String productName=request.getParameter("productName");
		String productEfficacy=request.getParameter("productEfficacy");
		String productOrig=request.getParameter("productOrig");
		
		int beginProductPrice=0;
		if(request.getParameter("beginProductPrice")!=null && !request.getParameter("beginProductPrice").equals("")) {
			beginProductPrice=Integer.parseInt(request.getParameter("beginProductPrice"));
		}
		int endProductPrice=0;
		if(request.getParameter("endProductPrice")!=null && !request.getParameter("endProductPrice").equals("")) {
			endProductPrice=Integer.parseInt(request.getParameter("endProductPrice"));
		}
		
		String productCurrent=request.getParameter("productCurrent");
		String brand=request.getParameter("brand");
		String customize=request.getParameter("customize");
		int productNorms=0;
		if(request.getParameter("productNorms")!=null && !request.getParameter("productNorms").equals("")) {
			productNorms=Integer.parseInt(request.getParameter("productNorms"));
		}
		
		String productElement=request.getParameter("productElement");
		int productCatalogueId=0;
		if(request.getParameter("productCatalogueId")!=null && !request.getParameter("productCatalogueId").equals("")) {
			productCatalogueId=Integer.parseInt(request.getParameter("productCatalogueId"));
		}
		
		String productImg=request.getParameter("productImg");
		//根据接收的参数创建产品对象
		Products p=new Products(productId, productName, productEfficacy, productOrig, beginProductPrice, endProductPrice, productCurrent, brand, customize, productNorms, productElement, productCatalogueId, productImg);
		int page=0;
		if(request.getParameter("page")!=null && !request.getParameter("page").equals("")) {
			page=Integer.parseInt(request.getParameter("page"));
		}
		int size=0;
		if(request.getParameter("size")!=null && !request.getParameter("size").equals("")) {
			size=Integer.parseInt(request.getParameter("size"));
		}
		XiangQingDaoImpl pDao=new XiangQingDaoImpl();//创建实现类对象
		List<Products> pList=pDao.getproductsByWherePage(p, page, size);//调用实现类中的多条件方法
		
		response.setCharacterEncoding("UTF-8");//设置响应编码集格式
		PrintWriter out=response.getWriter();//创建out对象
		
		Gson gson=new Gson();//创建gson对象
		
		String res=gson.toJson(pList);//将pcList转化为json字符串
		out.print(res);//响应数据
		
		out.flush();
		out.close();
	}
	
	public void getAllProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接收前台传来的参数
		int productId=0;
		if(request.getParameter("productId")!=null && !request.getParameter("productId").equals("")) {
			productId=Integer.parseInt(request.getParameter("productId"));
		}
			
		String productName=request.getParameter("productName");
		String productEfficacy=request.getParameter("productEfficacy");
		String productOrig=request.getParameter("productOrig");
		
		int beginProductPrice=0;
		if(request.getParameter("beginProductPrice")!=null && !request.getParameter("beginProductPrice").equals("")) {
			beginProductPrice=Integer.parseInt(request.getParameter("beginProductPrice"));
		}
		int endProductPrice=0;
		if(request.getParameter("endProductPrice")!=null && !request.getParameter("endProductPrice").equals("")) {
			endProductPrice=Integer.parseInt(request.getParameter("endProductPrice"));
		}
		
		String productCurrent=request.getParameter("productCurrent");
		String brand=request.getParameter("brand");
		String customize=request.getParameter("customize");
		int productNorms=0;
		if(request.getParameter("productNorms")!=null && !request.getParameter("productNorms").equals("")) {
			productNorms=Integer.parseInt(request.getParameter("productNorms"));
		}
		
		String productElement=request.getParameter("productElement");
		int productCatalogueId=0;
		if(request.getParameter("productCatalogueId")!=null && !request.getParameter("productCatalogueId").equals("")) {
			productCatalogueId=Integer.parseInt(request.getParameter("productCatalogueId"));
		}
		
		String productImg=request.getParameter("productImg");
		//根据接收的参数创建产品对象
		Products p=new Products(productId, productName, productEfficacy, productOrig, beginProductPrice, endProductPrice, productCurrent, brand, customize, productNorms, productElement, productCatalogueId, productImg);
		
		XiangQingDaoImpl pDao=new XiangQingDaoImpl();//创建实现类对象
		List<Products> pList=pDao.getproductsByWhere(p);//调用实现类中的多条件方法
		
		response.setCharacterEncoding("UTF-8");//设置响应编码集格式
		PrintWriter out=response.getWriter();//创建out对象
		
		Gson gson=new Gson();//创建gson对象
		
		String res=gson.toJson(pList.size());//将pcList转化为json字符串
		out.print(res);//响应数据
		
		out.flush();
		out.close();
	}
    /****
     * 删除产品
     */
	public void delProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//接收前台传来的参数
		int id=Integer.parseInt(request.getParameter("productId"));
		XiangQingDaoImpl pDao=new XiangQingDaoImpl();//创建实现类
		
		response.setCharacterEncoding("UTF-8");//设置编码集格式
		PrintWriter out=response.getWriter();
		
		int n=pDao.delProduct(id);//调用实现类的删除方法，返回受影响
		if(n>0) {//删除成功
			out.print(Result.toClient(true, "删除成功",""));
		}else {//删除失败
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		out.flush();
		out.close();
	}
	/***
	 * localhost:8080/huaduoyoupin/adminProduct?reqName=addProduct
	 * 添加产品
	 * 
	 */
	public void addProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		XiangQingDaoImpl pDao=new XiangQingDaoImpl();//创建实现类
		
		response.setCharacterEncoding("UTF-8");//设置编码集格式
		PrintWriter out=response.getWriter();
		
		String productName=request.getParameter("productName");
		String productEfficacy=request.getParameter("productEfficacy");
		String productOrig=request.getParameter("productOrig");
		String productCurrent=request.getParameter("productCurrent");
		String brand=request.getParameter("brand");
		String customize=request.getParameter("customize");
		int productNorms=0;
		if(request.getParameter("productNorms")!=null && !request.getParameter("productNorms").equals("")) {
			productNorms=Integer.parseInt(request.getParameter("productNorms"));
		}
		String productElement=request.getParameter("productElement");
		int productCatalogueId=0;
		if(request.getParameter("productCatalogueId")!=null && !request.getParameter("productCatalogueId").equals("")) {
			productCatalogueId=Integer.parseInt(request.getParameter("productCatalogueId"));
		}
		String productImg=request.getParameter("productImg");
		if(productName!=null&&!productName.equals("")) {
			List<Products> pList=pDao.getProductByName(productName);
			if(pList.size()==0) {
				Products p=new Products(0, productName, productEfficacy, productOrig,productCurrent, brand, customize, productNorms, productElement, productCatalogueId, productImg);
				int n=pDao.addProduct(p);//调用实现类的添加方法，返回受影响
				if(n>0) {
					out.print(Result.toClient(true, "添加成功",""));
				}else {
					out.print(Result.toClient(false, "添加失败",""));
				}
			}else {
				out.print(Result.toClient(false, "产品名称已存在",""));
			}
		}
		
		
		out.flush();
		out.close();
	}
	
	/***
	 *  localhost:8080/huaduoyoupin/adminProduct?reqName=getProductsById
	 */
    public void getProductsById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		XiangQingDaoImpl pDao=new XiangQingDaoImpl();//创建实现类
		
		response.setCharacterEncoding("UTF-8");//设置编码集格式
		PrintWriter out=response.getWriter();
		
		int pId=Integer.parseInt(request.getParameter("productId"));
		Products p=pDao.getProductsById(pId);
		
		Gson gson=new Gson();
		String res=gson.toJson(p);
		out.print(res);
		
		out.flush();
		out.close();
	}
	/***
	 * 修改产品内容
	 * localhost:8080/huaduoyoupin/adminProduct?reqName=updateProductsById
	 */
     public void updateProductsById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
			XiangQingDaoImpl pDao=new XiangQingDaoImpl();//创建实现类
			
			response.setCharacterEncoding("UTF-8");//设置编码集格式
			PrintWriter out=response.getWriter();
			int productId=0;
			if(request.getParameter("productId")!=null && !request.getParameter("productId").equals("")) {
				productId=Integer.parseInt(request.getParameter("productId"));
			}
			String productName=request.getParameter("productName");
			String productEfficacy=request.getParameter("productEfficacy");
			String productCurrent=request.getParameter("productCurrent");
			
			String productElement=request.getParameter("productElement");
			int productCatalogueId=0;
			if(request.getParameter("productCatalogueId")!=null && !request.getParameter("productCatalogueId").equals("")) {
				productCatalogueId=Integer.parseInt(request.getParameter("productCatalogueId"));
			}
			String productImg=request.getParameter("productImg");
			if(productName!=null&&!productName.equals("")) {
				List<Products> pList=pDao.getProductByName(productName);
				if(pList.size()==0) {
					Products p=new Products(productId, productName, productEfficacy,productCurrent,productElement,productCatalogueId, productImg);
					int n=pDao.updateProductsById(p);//调用实现类的添加方法，返回受影响
					if(n>0) {
						out.print(Result.toClient(true, "添加成功",""));
					}else {
						out.print(Result.toClient(false, "添加失败",""));
					}
				}else {
					out.print(Result.toClient(false, "产品名称已存在",""));
				}
			}
			
			
			out.flush();
			out.close();
	}
    //批量删除
    //localhost:8080/huaduoyoupin/adminProduct?reqName=delectProductByIds
     public void delectProductByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
 		XiangQingDaoImpl pDao=new XiangQingDaoImpl();//创建实现类
 		
 		response.setCharacterEncoding("UTF-8");//设置编码集格式
 		PrintWriter out=response.getWriter();
 		String ids=request.getParameter("ids");
 		int n=pDao.delectProductByIds(ids);
 		//调用实现类的删除方法，返回受影响
 		if(n>0) {//删除成功
 			out.print(Result.toClient(true, "删除成功",""));
 		}else {//删除失败
 			out.print(Result.toClient(false, "删除失败",""));
 		}
 		
 		out.flush();
 		out.close();
 	}
}
